﻿
using NPOI.SS.Formula.Functions;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MeiTool.Common
{
    public class ExcelHelp
    { 
        public static void DataToExcel<T>(List<T> Datas, List<string> Titles, List<string> Columns, string SheetName, string ExcelFileSavePath, string ExcelFileName)
        {
            try
            {
                //文件名
                string filePath = ExcelFileSavePath + "\\" + ExcelFileName + (ExcelFileName.EndsWith(".xlsx") ? "" : ".xlsx");
                //文件是否存在
                bool exists = File.Exists(filePath);
                if (exists)
                {
                    filePath = ExcelFileSavePath + "\\" + ExcelFileName +
                        (ExcelFileName.EndsWith(".xlsx") ? ExcelFileName.Replace(".xlsx", $"_{DateTime.Now:ssffff}.xlsx") : $"_{DateTime.Now:ssffff}.xlsx");
                }
                XSSFWorkbook book = new XSSFWorkbook();
                NPOI.SS.UserModel.ISheet sheet = book.CreateSheet(SheetName);

                NPOI.SS.UserModel.IRow firstRow = sheet.CreateRow(0);
                for (int i = 0; i < Titles.Count; i++)
                {
                    firstRow.CreateCell(i).SetCellValue(Titles[i]);
                }

                int cellRow = 1;
                int cellColumn = 0;
                foreach (var data in Datas)
                {
                    NPOI.SS.UserModel.IRow row = sheet.CreateRow(cellRow);
                    foreach (var column in Columns)
                    {
                        var dd = column;
                        //使用反射动态获取属性值
                        var type = data.GetType();
                        var propertyInfo = type.GetProperty(column);
                        var propertyValue = propertyInfo.GetValue(data);

                        if (propertyValue == null)
                        {
                        }
                        else if (propertyInfo.PropertyType == typeof(long) ||
                            propertyInfo.PropertyType == typeof(int) ||
                            propertyInfo.PropertyType == typeof(double) ||
                            propertyInfo.PropertyType == typeof(decimal) ||
                            propertyInfo.PropertyType == typeof(float))
                        {
                            row.CreateCell(cellColumn).SetCellValue(Convert.ToDouble(propertyValue));
                        }
                        else if ((propertyInfo.PropertyType == typeof(DateTime) || propertyInfo.PropertyType == typeof(DateTime?)))
                        {
                            var time = Convert.ToDateTime(propertyValue);

                            row.CreateCell(cellColumn).SetCellValue(time.ToString());
                        }
                        else if (propertyInfo.PropertyType == typeof(bool) || propertyInfo.PropertyType == typeof(bool?))
                        {
                            if (true == Convert.ToBoolean(propertyValue))
                            {
                                row.CreateCell(cellColumn).SetCellValue("是");
                            }
                            else
                            {
                                row.CreateCell(cellColumn).SetCellValue("否");
                            }
                        }
                        else
                        {
                            row.CreateCell(cellColumn).SetCellValue(Convert.ToString(propertyValue));
                        }

                        cellColumn++;
                    }
                    cellColumn = 0;
                    cellRow++;
                }

                book.Write(new FileStream(filePath, FileMode.Create, FileAccess.Write));//write后，会关闭ms流 

            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }

        }
    }
}
